﻿// ---------------------------------------------------------------------------------------
package iso {
	
	// FLASH IMPORTS
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.geom.Point;
	
	
	// -----------------------------------------------------------------------------------
	public class Iso {
		
		// tile dimensions
		private var _uiTileSize:uint;
		
		// all that math stuff
		private var nTheta:Number;
		private var nAlpha:Number;
		private var sinTheta:Number;
		private var cosTheta:Number;
		private var sinAlpha:Number;
		private var cosAlpha:Number;

		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		public function Iso( tileSize:uint ) {

			_uiTileSize = tileSize;
			
			nTheta = 30;
			nAlpha = 45;
			nTheta *= Math.PI / 180;
			nAlpha *= Math.PI / 180;
			
			sinTheta = Math.sin(nTheta);
			cosTheta = Math.cos(nTheta);
			sinAlpha = Math.sin(nAlpha);
			cosAlpha = Math.cos(nAlpha);
		}

		// -------------------------------------------------------------------------------
		// CONVERT 2D POS TO ISO POS
		// -------------------------------------------------------------------------------
		public function getIsoPos( col:int, row:int ):Point {
			
			var x:int = ( col * _uiTileSize );
			var z:int = ( row * _uiTileSize ) * -1;

			var yp:Number = 0; // we're not using height in this game
			var xp:Number = (x * cosAlpha) + (z * sinAlpha);
			var zp:Number = (z * cosAlpha) - (x * sinAlpha);
			
			var xPos:Number = xp;
			var yPos:Number = (yp * cosTheta) - (zp * sinTheta);
			
			return ( new Point(xPos,yPos) );
		}

		// -------------------------------------------------------------------------------
		// CONVERT ISO POS TO 2D POS
		// -------------------------------------------------------------------------------
		public function map2Iso( screenX:Number, screenY:Number ):Point {
			
			var z:Number = (screenX / cosAlpha - screenY / (sinAlpha * sinTheta)) * (1 / (cosAlpha / sinAlpha + sinAlpha / cosAlpha));
			var x:Number = (1 / cosAlpha) * (screenX - z * sinAlpha);
						
			return ( new Point(x,z) );
		}
		
		// -------------------------------------------------------------------------------
		// DRAW ISO GROUND MOVIECLIP
		// -------------------------------------------------------------------------------
		public function drawIsoGround( mcParent:MovieClip, uiRows:uint, uiCols:uint ):void {
											 
			var uiColor1:uint = 0xAAAAAA;
			var uiColor2:uint = 0x999999;
			
			var uiTileStart:uint;
			var uiTileFrame:uint;
			
			var mcGround:MovieClip = new MovieClip();
			var canvas:Shape = new Shape();
			
			for ( var row:uint = 0; row < uiRows; row++ ) {
				
				uiTileStart = (uiTileStart == uiColor1) ? uiColor2 : uiColor1;
				uiTileFrame = uiTileStart;
				
				for ( var col:uint = 0; col < uiCols; col++ ) {
			
					canvas.graphics.beginFill( uiTileFrame );
					canvas.graphics.drawRect( (col * _uiTileSize), (row * _uiTileSize), _uiTileSize, _uiTileSize );
					canvas.graphics.endFill();
					
					uiTileFrame = (uiTileFrame == uiColor1) ? uiColor2 : uiColor1;
				}
			}

			// make it iso!
			canvas.rotation = 45;
			mcGround.addChild( canvas );
			mcGround.scaleY  = 0.5;
			
			mcGround.cacheAsBitmap = true;
			mcParent.addChild( mcGround );
		}
	}
}